package com.hns.can.energy.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.ReportAbnorEnergyStatisService;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.PubUtil;

/**
 * 异常能耗
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/reportAbnorEnergy")
public class ReportAbnorEnergyStatisController extends BaseController{
    @Autowired
    ReportAbnorEnergyStatisService reportAbnorEnergyService;
    
    final private String[] headerNames = new String[]{"车牌号","驾驶员","运行线路","所属机构","能源类型","发生时间",
        "发生地点"};
    final private String[] headerFields = new String[]{"licPltNo", "drvName", "lineName", 
        "organName", "carType", "mftOccurTime", "happenlocation"};

    /**
     * 异常能耗查询
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/searchReportAbnorEnergyStatis")
    @ResponseBody
    public Object searchReportAbnorEnergyStatis(CommonParamsVo vo) throws Exception {
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        Pagination page = reportAbnorEnergyService.searchReportAbnorEnergyStatis(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 异常能耗导出excel                
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportReportAbnorEnergyStatisExcel")
    public void exportReportAbnorEnergyStatisExcel(CommonParamsVo vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List list = reportAbnorEnergyService.exportReportAbnorEnergyStatis(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, "car_energy");
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }
    
    /**
     * 异常能耗导出PDF
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportReportAbnorEnergyStatisPdf")
    public void exportReportAbnorEnergyStatisPdf(CommonParamsVo vo, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List list = reportAbnorEnergyService.exportReportAbnorEnergyStatis(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, "car_energy");
        
        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
}
